$(document).ready(function(){
    $('#resetPassword-form').on('submit', function(event) {
        event.preventDefault(); // 阻止表单的默认提交行为

        var original_password = $('#original_password').val();
        var new_password = $('#new_password').val();
        var repeat_new_password = $('#repeat_new_password').val();

        // 做一些基本的输入验证
        if(original_password === '' || new_password === '' || repeat_new_password === '') {
            $('#error-message').text('请输入完整信息');
            return;
        }

        if(new_password != repeat_new_password){
            $('#error-message').text('新密码输入不一致');
            return;
        }

        // 通过 AJAX 提交
        $.ajax({
            url: '/api/v1/password',
            type: 'PUT',
            data: {
                original_password: original_password,
                new_password: new_password
            },
            success: function(result) {
                var data = result;
                console.log(data);
                console.log(data.success);
                if (data.success == 1) {
                    console.log('1111');
                    $('#message').text('更改成功, 请重新登录');
                    setTimeout("window.location.href='/login';", 500);
                } else {
                    console.log('2222');
                    $('#error-message').text(data.msg);
                }
            },
            error: function(error) {
                $('#error-message').text('请求失败, 请稍后再试。');
            }
        });
    });
});